package com.lineage.data.item_etcitem.dragon;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.serverpackets.S_ItemName;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;

public class HatchlingEgg_Green extends ItemExecutor {
  public static ItemExecutor get() {
    return new HatchlingEgg_Green();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (item == null)
      return; 
    if (pc == null)
      return; 
    int petcost = 0;
    Object[] petList = pc.getPetList().values().toArray();
    if (petList.length >= 4) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(489));
      return;
    } 
    Object[] array;
    int length = (array = petList).length;
    int i = 0;
    while (i < length) {
      Object pet = array[i];
      int nowpetcost = ((L1NpcInstance)pet).getPetcost();
      petcost += nowpetcost;
      i++;
    } 
    int charisma = pc.getCha();
    if (pc.isCrown()) {
      charisma += 6;
    } else if (!pc.isKnight()) {
      if (pc.isElf()) {
        charisma += 12;
      } else if (pc.isWizard()) {
        charisma += 6;
      } else if (pc.isDarkelf()) {
        charisma += 6;
      } else if (pc.isDragonKnight()) {
        charisma += 6;
      } else if (pc.isIllusionist()) {
        charisma += 6;
      } 
    } 
    charisma -= petcost;
    if (charisma <= 0) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(489));
      return;
    } 
    L1PcInventory inv = pc.getInventory();
//    if (inv.getSize() < 180) {
//      L1ItemInstance petamu = inv.storeItem(40314, 1L);
//      if (petamu != null) {
//        pc.sendPackets((ServerBasePacket)new S_ItemName(petamu));
//        pc.getInventory().removeItem(item, 1L);
//      } 
//    } 
	if (inv.getSize() < 180) {
		final L1ItemInstance petamu = inv.storeItem(40314, 1); // 項圈
		if (petamu != null) {
			new L1PetInstance(97023, pc, petamu.getId());
			pc.sendPackets(new S_ItemName(petamu));

			// 刪除道具
			pc.getInventory().removeItem(item, 1);
		}
	}
  }
}
